package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.user.LoginVo;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/19 15:33
 * @Version 1.0
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    //  调用mapper 层
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo login(LoginVo loginVo) {
        //  select * from user_info where (login_name = ? or email = ? or phone_num = ?) and passwd = ?;
        //  微信扫码登录 PC 端
//        String passwd = loginVo.getPasswd();
//        String newPwd = MD5.encrypt(passwd);


        //  构建查询条件：PC 端

//        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        userInfoLambdaQueryWrapper.eq(UserInfo::getPasswd,newPwd);
        //  (login_name = ? or email = ? or phone_num = ?) 动态sql? if() loginVo.getLoginName();
//        userInfoLambdaQueryWrapper.and(query->{
//            query.eq(UserInfo::getLoginName,loginVo.getLoginName()).or()
//                    .eq(UserInfo::getEmail,loginVo.getLoginName()).or()
//                    .eq(UserInfo::getPhoneNum,loginVo.getLoginName());
//        });


        //  移动端：
        String passwd = MD5.encrypt(loginVo.getPassword());
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfoLambdaQueryWrapper.eq(UserInfo::getPasswd,passwd);

        userInfoLambdaQueryWrapper.and(query->{
            query.eq(UserInfo::getLoginName,loginVo.getPhone()).or()
                    .eq(UserInfo::getEmail,loginVo.getPhone()).or()
                    .eq(UserInfo::getPhoneNum,loginVo.getPhone());
        });
        UserInfo info = userInfoMapper.selectOne(userInfoLambdaQueryWrapper);
        //  返回登录对象
        return info;
    }
}
